package com.sword.pay.alipay.interfaces.facade;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.domain.AlipayTradePagePayModel;
import com.alipay.api.domain.ExtendParams;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.sword.common.bean.Result;
import com.sword.common.utils.PayUtil;
import com.sword.pay.alipay.domain.config.AliPayConfig;
import com.sword.pay.alipay.domain.bo.AlipayParam;
import com.sword.pay.alipay.infrastructure.AliPayApi;
import com.sword.pay.bean.bo.PayOrder;
import com.sword.pay.bean.enums.PayMethodEnum;

import java.util.Objects;

/**
 * @description: 阿里支付
 * @author：Lv.RJ
 * @version: 1.0.0
 * @create: 2025/02/18
 **/
public class AliPayFacade {
//    private static final Log log = LogFactory.getLogger(AliPayFacade.class);
    public static Result pay(PayOrder payOrder, AlipayParam aliPayParam, AliPayConfig aliPayConfig) {
        Result<Object> payResult = new Result<>();

        String amount = PayUtil.toDecimal(payOrder.getAmount()).toString();
        String payBody = null;

        // 异步线程存储
        // pc支付
        if (Objects.equals(payOrder.getMethod(), PayMethodEnum.WEB)) {
            payBody = webPay(amount, payOrder, aliPayConfig);
        }
/*        // 程序支付
        else if (Objects.equals(payOrder.getMethod(), PayMethodEnum.APP.getCode())) {
            payBody = this.appPay(amount, payOrder, aliPayConfig);
        }
        // wap支付
        else if (Objects.equals(payOrder.getMethod(), PayMethodEnum.WAP.getCode())) {
            payBody = this.wapPay(amount, payOrder, aliPayConfig);
        }


        // jsapi支付
        else if (Objects.equals(payOrder.getMethod(), PayMethodEnum.JSAPI.getCode())) {
            payBody = this.jsapiPay(amount, payOrder, aliPayParam, aliPayConfig);
        }
        // 二维码支付
        else if (Objects.equals(payOrder.getMethod(), PayMethodEnum.QRCODE.getCode())) {
            payBody = this.qrCodePay(amount, payOrder, aliPayConfig);
        }
        // 付款码支付, 付款码存在直接支付成功的情况, 所以返回结果特殊处理
        else if (Objects.equals(payOrder.getMethod(), PayMethodEnum.BARCODE.getCode())) {
            this.barCode(amount, payOrder, aliPayParam, payResult, aliPayConfig);
            return payResult;
        }*/
        // 通常是发起支付的参数
        payResult.setData(payBody);
        return payResult;
    }

    public static String webPay(String amount, PayOrder payOrder, AliPayConfig aliPayConfig) {

        // 获取支付宝客户端
//        AlipayClient alipayClient = aliPayConfigService.getAlipayClient(aliPayConfig);
        AlipayTradePagePayModel model = new AlipayTradePagePayModel();
        model.setSubject(payOrder.getTitle());
        model.setOutTradeNo(payOrder.getOrderNo());
        // 过期时间
//        model.setTimeExpire(this.getAliTimeExpire(payOrder.getExpiredTime()));
        model.setTotalAmount(amount);
        // 目前仅支持FAST_INSTANT_TRADE_PAY
//        model.setProductCode(AlipayCode.Products.FAST_INSTANT_TRADE_PAY);

        // 是否分账
        if (payOrder.getAllocation()) {
            ExtendParams extendParams = new ExtendParams();
            extendParams.setRoyaltyFreeze(Boolean.TRUE.toString());
            model.setExtendParams(extendParams);
        }
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        // 特约商户调用
        /*if (aliPayConfig.isIsv()){
            request.putOtherTextParam(AlipayConstants.APP_AUTH_TOKEN, aliPayConfig.getAppAuthToken());
        }*/
        request.setBizModel(model);
        // 异步回调必须到当前系统中
//        request.setNotifyUrl(aliPayConfigService.getNotifyUrl(aliPayConfig.isIsv()));
        // 同步回调
//        request.setReturnUrl(aliPayConfigService.getReturnUrl(aliPayConfig.isIsv()));
        try {
//            AlipayClient alipayClient =null;
//            AlipayTradePagePayResponse get = alipayClient.pageExecute(request, "GET");
            // 通过GET方式的请求, 返回URL的响应, 默认是POST方式的请求, 返回的是表单响应
            AlipayTradePagePayResponse response = AliPayApi.pageExecute(request, "GET");
//            AlipayTradePagePayResponse response = alipayClient.pageExecute(request, HttpMethod.GET.name());
            return response.getBody();
        } catch (AlipayApiException e) {
//            log.error("支付宝PC支付失败", e);
            throw new RuntimeException("支付宝PC支付失败: " + e.getMessage());
        }
    }
}
